package com.lswms.modules.lswms.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.dao.base.CargojcDao;
import com.lswms.modules.lswms.dao.base.JianceDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.stock.LotDao;
import com.lswms.modules.lswms.dto.base.JianceDTO;
import com.lswms.modules.lswms.dto.stock.InventoryDTO;
import com.lswms.modules.lswms.entity.base.CargojcEntity;
import com.lswms.modules.lswms.entity.base.JianceEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.stock.LotEntity;
import com.lswms.modules.lswms.service.base.JianceService;
import com.lswms.modules.lswms.service.base.SequenceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 货物检测
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-12-30
 */
@Service
public class JianceServiceImpl extends CrudServiceImpl<JianceDao, InventoryEntity, InventoryDTO> implements JianceService {
    @Autowired
    private JianceDao jiance;

    @Autowired
    private LotDao lotDao;

    @Autowired
    private CargojcDao cargo;

    @Autowired
    SequenceService sequenceService;

    @Autowired
    private InventoryDao inventoryDao;
    @Override
    public QueryWrapper<InventoryEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<InventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    //查询
    @Override
    public String detectionSelect(String id) {
        //
        QueryWrapper<InventoryEntity> wrappernIvent = new QueryWrapper<>();
        wrappernIvent.eq("id",id);
        InventoryEntity invent = inventoryDao.selectOne(wrappernIvent);
        QueryWrapper<LotEntity> wrapperLot = new QueryWrapper<>();
        wrapperLot.eq("sku",invent.getSku());
        wrapperLot.eq("lot15","检测");
        List<LotEntity> lotEntityList = lotDao.selectList(wrapperLot);
        LotEntity lotEntity = lotDao.selectOne(wrapperLot);
        if(lotEntityList.size()<1){

            //如果没有，就新建一个LOT记录
            QueryWrapper<LotEntity> wrapperLot2 = new QueryWrapper<>();
            wrapperLot2.orderByDesc("lot");
            List<LotEntity> lotEntityList1 = lotDao.selectList(wrapperLot2);

            LotEntity lotEntity2 = lotEntityList1.get(0);
            String lot = sequenceService.getSequence("lot");//lot字段的值

            QueryWrapper<LotEntity> wrapperLot3 = new QueryWrapper<>();
            wrapperLot3.eq("lot",invent.getLot());
            LotEntity lotEntity3 = lotDao.selectOne(wrapperLot3);
            lotEntity3.setLot(lot);
            lotEntity3.setLot15("检测");
            lotDao.insert(lotEntity3);
//            invent.setLot(lot);
//            inventoryDao.updateById(invent);
            jiance.detectionUpdate(lot,id);

            //return "开始检测！";
        }

        //查询cargojc表有没有这个箱子，没有就新增进去，有就不加了
        QueryWrapper<CargojcEntity> cargojcEntity = new QueryWrapper<>();
        cargojcEntity.eq("cargono",invent.getTray());
        List<CargojcEntity> listCargo = cargo.selectList(cargojcEntity);
        if(listCargo.size()<1){
            //检测箱里没有当前的这个记录，新增一条进去
            CargojcEntity cargoe = new CargojcEntity();
            cargoe.setCargoNo(invent.getTray());//箱号
            cargoe.setLocation(invent.getLocation());//箱子位置
            cargoe.setPid(invent.getId());
            //SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
            cargoe.setUpdateTime(new Date());
            cargo.insert(cargoe);
        }else{
            //有了就不用新增了
        }

        if (lotEntity.getLot().equals(invent.getLot())){
            return "当前货物正在检测！请不要重复操作";
        }else{
//            invent.setLot(lotEntity.getLot());
//            inventoryDao.updateById(invent);
            jiance.detectionUpdate(lotEntity.getLot(),id);
            return "开始检测！";
        }

        //数据为空返回（预防空指针）
//        if(baseDao.detectionSelect(wrapper)==null){
//
//            Map<String , Object> map = new HashMap<>();
//            map.put("lot15","数据为null");
//            list.add(map);
//            return list;
//
//        }
//         list = baseDao.detectionSelect(wrapper);
//        if(list.contains("检测")){
//            return list;
//        }else{
//            return null;
//        }
    }

    //新增
    @Override
    public void detectionInsert(Map<String,Object> map) {
        jiance.detectionInsert(map);
    }

    //修改
//    @Override
//    public void detectionUpdate(String lot,String id) {
//        jiance.detectionUpdate(lot,id);
//    }


}